package cn.bdqn.controller;

import cn.bdqn.domain.OrderForm;
import cn.bdqn.service.OrderFormService;
import cn.bdqn.utils.result.Result;
import cn.bdqn.utils.result.ResultGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 订单货物配货控制层
 * @author dkj
 */
@RestController
public class OrderFormController {


    /**
     * 创建日志记录器
     */
    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private OrderFormService orderFormService;

    /**
     * 修改订单货物对象
     * @param orderForm
     * @return
     */
    @PutMapping("/orderFrom/update/")
    public Result updateOrderFrom(@RequestBody OrderForm orderForm){
        try {
            logger.info("进入了方法,参数：orderForm={}",orderForm);
            //修改
            OrderForm orderForm1 = orderFormService.updateOrderFrom(orderForm);
            //返回修改后的结果
            return ResultGenerator.genSuccessResult(orderForm1);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("报错了："+e.getMessage());
            return ResultGenerator.genFailResult("修改失败");
        }
    }


    /**
     * 新增订单货物id
     * @param orderForms
     * @return
     */
    @PostMapping("/orderFrom/save/")
    public Result save(@RequestBody List<OrderForm> orderForms){
        try {
            logger.info("进入了方法，参数：orderForms={}",orderForms);
            List<OrderForm> orderFormList = new ArrayList<>();
            orderForms.forEach(orderForm -> {
                //添加
                OrderForm orderForm1 = orderFormService.saveOrderForm(orderForm);
                orderFormList.add(orderForm1);
            });
            return ResultGenerator.genSuccessResult(orderFormList);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("报错了："+e.getMessage());
            return ResultGenerator.genFailResult("错误");
        }
    }

}
